package com.sixmac;

import com.google.common.base.Strings;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.sixmac.core.toolbox.kit.StrKit;
import com.sixmac.core.toolbox.support.Convert;
import org.junit.Test;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by admin on 2018/7/19.
 */
public class GlobalInterceptor implements HandlerInterceptor {
    public static Cache<String, Object> cache;

    static {
        cache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(1, TimeUnit.MINUTES).removalListener(new RemovalListener<String, Object>() {
            @Override
            public void onRemoval(RemovalNotification<String, Object> removalNotification) {
                System.out.println("被移除缓存key:" + removalNotification.getKey() + "value:" + removalNotification.getValue() + "cause:" + removalNotification.getCause());
            }
        }).build();

    }


    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String token = Convert.toStr(httpServletRequest.getParameter("token"));
        if (token != null && !token.equals("")) {
            cache.put(token, 1);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

    @Test
    public void te() {
        List<String> lists = Lists.newArrayList();
        Set<Integer> set1 = Sets.newHashSet(1, 2, 3, 4, 5, 6);
        System.out.print(set1);


    }

}
